/*
 * player.cpp - multimedia player GUI
 *
 * Copyright (C) 2010-2011 Serge Begger
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "player.h"
#include "ui_player.h"

player::player(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::player)
{
    ui->setupUi(this);
    ui->timeLcd->display("00:00");

    timer = new QTimer(this);

    playlist = new pllist(this);
    playlist->hide();
    isPlayed = false;

    connect(timer, SIGNAL(timeout()), this, SLOT(updateSlider()));
    connect(playlist, SIGNAL(pllistHide()),this, SLOT(unHide()));
    connect(playlist, SIGNAL(stopPlay()),this, SLOT(on_stopButton_pressed()));
    connect(playlist, SIGNAL(startPlay()),this, SLOT(on_playButton_pressed()));

    gstEngine = new gstengine(this);

    settings = new QSettings("carpc", "player", this);
    settings->setPath(QSettings::IniFormat,QSettings::UserScope,"carpc");
    currVolume = settings->value("currVolume").toInt();
    currTrack = settings->value("currTrack").toInt();
    isPrevPlay = settings->value("isPrevPlay").toBool();
    if (currTrack != 0) {
        playlist->setCurrTrack(currTrack);
    }
}

player::~player()
{
    settings->setValue("currTrack"  , currTrack);
    settings->setValue("isPrevPlay" , isPlayed);
    stop();
    gstEngine->~gstengine();
    delete ui;
}

void player::setVolume(int vol) {
    currVolume = vol;
    gstEngine->setVolume(vol);
    settings->setValue("currVolume"  , currVolume);
}

void player::init()
{
    if (currTrack != 0 && isPrevPlay)
        on_playButton_pressed();
}

void player::playPath(QString path)
{
    playlist->addPath(path);
    on_playButton_pressed();
    emit hideWidget();
}

void player::on_backButton_pressed()
{
    emit hideWidget();
}

void player::on_closeButton_pressed()
{
    this->~player();
}

void player::on_pllistButton_pressed()
{
    ui->framePlayer->hide();
    playlist->show();
}

void player::on_playButton_pressed()
{
    if (!isPlayed)
    {
        if(setTrack())           
            play();
    } else {
        pause();
    }
}

void player::on_stopButton_pressed()
{
        stop();
}

void player::on_nextButton_pressed()
{
    stop();
    currTrack++;
    playlist->setCurrTrack(currTrack);
    if (setTrack())
        play();
}

void player::on_prevButton_pressed()
{
    stop();
    currTrack--;
    playlist->setCurrTrack(currTrack);
    if (setTrack())
        play();
}

void player::unHide()
{
    if (ui->framePlayer->isHidden())
        ui->framePlayer->show();
}

bool player::setTrack()
{
    gstEngine->stopPlayer();
    currTrack = playlist->currTrack;
    ui->positionSlider->setMaximum(0);
    if(currTrack != 0)
    {
        QString fileName = playlist->getCurrFileName();
        ui->labelTitle->setText("Title: " + playlist->getCurrTitle());
        ui->labelArtist->setText("Artist: " + playlist->getCurrArtist());
        ui->labelAlbum->setText("Album: " + playlist->getCurrAlbum());
        ui->labelTime->setText("Time: " + playlist->getCurrLength());
        ui->labelYear->setText(QString("Year: %0").arg(playlist->getCurrYear()));

        isFlacApe = playlist->isFlacApe();
        if (isFlacApe) {
            startTime = playlist->getStartTime();
            lenTime = playlist->getLenTime();
        }

        ui->trackLcd->display(currTrack);
        ui->positionSlider->setSliderPosition(0);

        if (!gstEngine->initPlayer(ui->framePlayer,fileName)) {
            return false;
        }
        return true;
    }
    return false;
}

void player::play()
{
    if (gstEngine->isVideo()) {
        gstEngine->currGeometry = QRect(0,175,791,330);
        ui->frameMusic->hide();
    } else {
        ui->frameMusic->show();
    }

    if (!gstEngine->playFile())        
        return;

    int i = 0;
    if (isFlacApe) {
        while(!gstEngine->isPlayerPlayed()) {
            ++i;
            if (i > 1000) break;
        }
        gstEngine->seekPlayer(startTime);
    }

    if (gstEngine->isVideo()) {
        gstEngine->setVideoLCDtext(ui->labelTitle->text());
    }
    gstEngine->setVolume(currVolume);
    gstEngine->setFreq(currEQBand);
    ui->playButton->setIcon(QIcon(QString::fromUtf8(":/buttons/res/player_pause.png")));
    timer->start(200);
    isPlayed = true;
}

void player::stop()
{
    if (!isPlayed)
        return;
    if (timer->isActive())
        timer->stop();
    gstEngine->stopPlayer();
    isPlayed = false;
    ui->positionSlider->setValue(0);
    if (!gstEngine->isVideo())
        ui->timeLcd->display("00:00");
    ui->playButton->setIcon(QIcon(QString::fromUtf8(":/buttons/res/player_play.png")));
}

void player::pause()
{
    if (!gstEngine->isPlayerPlayed()) {
        ui->playButton->setIcon(QIcon(QString::fromUtf8(":/buttons/res/player_pause.png")));
    } else {
        ui->playButton->setIcon(QIcon(QString::fromUtf8(":/buttons/res/player_play.png")));
    }
    gstEngine->pausePlayer();
}


void player::on_positionSlider_sliderMoved(int position)
{
    if (isFlacApe) {
        position = startTime + position;
    }
    gstEngine->seekPlayer(position);
}

void player::on_positionSlider_actionTriggered(int action)
{
    if (action != 3)
        return;
    int position = ui->positionSlider->sliderPosition();
    if (isFlacApe) {
        position = startTime + position;
    }
    gstEngine->seekPlayer(position);
}

void player::updateSlider()
{
    if (!gstEngine->isPlayerPlayed())
        return;
    if (ui->positionSlider->maximum() == 0) {
        if (isFlacApe)
            ui->positionSlider->setMaximum(lenTime);
        else
            ui->positionSlider->setMaximum(gstEngine->getPlayerDuration());
    } else {
        qint64 time = gstEngine->getPlayerPosition() / GST_SECOND;
        if (isFlacApe) {
            time = time - startTime;
        }
        ui->positionSlider->setSliderPosition(time);
        ui->timeLcd->display(QTime(0,0,0,0).addSecs(time).toString("mm:ss"));
        if(ui->positionSlider->sliderPosition() == ui->positionSlider->maximum())
            on_nextButton_pressed();
    }
}
